<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <title>意达设计</title>
    <link rel="stylesheet" type="text/css" href="../css/api.css"/>
    <link rel="stylesheet" type="text/css" href="../css/dealer.css"/>
    <link rel="stylesheet" type="text/css" href="../css/member.css"/>
</head>
<body class="body">

<!-- header -->
<div class="pageheader"><a href="javascript:void(0);" onclick="api.closeWin();" class="return"></a><span>个人设置</span></div>

<!-- contact -->
<div class="pad">
	<div class="persli">
		<a href="#">
			<span>头像</span>
			<div class="persotx"><div class="personimg">
                <img src="" id='icon' onclick="showAction()">
                <input type="hidden" name='icon' id='iconid'>
            </div><i></i></div>
		</a>
	</div>
	<div class="persli memlist "><span>姓名</span><input type="text" class="txt" name="nickname" id='nickname'></div>
	<div class="persli memlist "><span>手机号码</span><input type="text" class="txt" name="phone" id='phone'></div>
	<div class="persli">
		<a href="javascript:;">
			<span>修改密码</span>
			<em onclick="xgpwd()"></em>
		</a>
	</div>
	<div class="persli memlist "><span>邮箱</span><input type="text" class="txt" name="email" id='email'></div>
	<div class="persli memlist "><span>地址</span><input type="text" class="txt" name="addr" id='addr'></div>

    <div class="logBnt"><input type="button" value="保存" onclick="tijiao()"></div>

    <div class="logBnt"><input type="button" value="退出" onclick="logout()" style="margin-top: 1rem"></div>
</div>



</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="../script/adaptive.js"></script>
<script type="text/javascript" src="../script/tabs.js"></script>
<script type="text/javascript">
    window['adaptive'].desinWidth = 750;
    window['adaptive'].baseFont = 28;
    window['adaptive'].maxWidth = 750;
    window['adaptive'].init();

    apiready = function () {
        $api.fixStatusBar($api.dom('.header'));
        api.addEventListener({
            name: 'keyback'
        }, function(ret, err){
            api.closeWidget();
        });
		api.showProgress();


		var para=api.pageParam;

		var p={token:token};
        api.ajax({
            url: webSiteUrl+'/app/user/user',
            method: 'post',
            data: {
                values:p
            }
        }, function(ret, err) {
            api.hideProgress();
            
			if(ret.code==200){
            	
                $('#icon').attr("src",webSiteUrl+ret.data.icon);
                $('#iconid').val(ret.data.iconid);

                $('#nickname').val(ret.data.nickname);
                $('#phone').val(ret.data.phone);
                $('#email').val(ret.data.email);
				$('#addr').val(ret.data.addr);
            }else{alert(ret.msg)}
        });


       
    }

    function tijiao() {
    	var nickname=$("input[name=nickname]").val();
        var phone=$("input[name=phone]").val();
        var email=$("input[name=email]").val();
        var addr=$("input[name=addr]").val();
        
        var icon=$("input[name=icon]").val();
        if(nickname==''){
            api.alert({msg:"请输入昵称！"});return;
        }else if(phone==''){
            api.alert({msg:"请输入手机号！"});return;
        }else if(email==''){
            api.alert({msg:"请输入邮箱！"});return;
        }else if(addr==''){
            api.alert({msg:"请输入地址！"});return;
        }

        var p={token:token,nickname:nickname,phone:phone,email:email,addr:addr,icon:icon};
        //alert(JSON.stringify(p));
        api.ajax({
            url: webSiteUrl+'/app/user/edit_user',
            method: 'post',
            data: {
                values:p
            }
        }, function(ret, err) {
            api.hideProgress();
            
			if(ret.code==200){
                
                api.openWin({
                    name: 'selectaddName',
                    url: './usercenter.html',
                    reload: true,
                    pageParam: {
                        
                    }
                });
            	
                /*$('#icon').attr("src",webSiteUrl+ret.data.icon);
                $('#name').html(ret.data.nickname);
                $('#phone').html(ret.data.phone);
                $('#email').html(ret.data.email);
				$('#addr').html(ret.data.addr);*/
            }else{alert(ret.msg)}
        });
    }

function xgpwd(){
    api.openWin({
        name: 'selectaddName',
        url: './xgpwd.html',
        pageParam: {
            
        }
    });
}
function logout(){
    $api.rmStorage('user');
    $api.clearStorage ();

    api.openWin({
        name: 'selectaddName',
        url: './login.html',
        pageParam: {
            
        }
    });
}

function showAction() {
     api.actionSheet({
         title: '上传图片',
         cancelTitle: '取消',
         buttons: ['拍照', '从手机相册选择']
     }, function(ret, err) {
         if (ret) {
             getPicture(ret.buttonIndex);
         }
     });
 }
function getPicture(sourceType) {
        if (sourceType == 1) { // 拍照
            //获取一张图片
            api.getPicture({
                sourceType: 'camera',
                encodingType: 'png',
                mediaValue: 'pic',
                allowEdit: false,
                quality: 90,
                saveToPhotoAlbum: true
            }, function(ret, err) {
                if(ret.data==''){return;}
                
                if (ret) {

                    var imgSrc = ret.data;
                    if (imgSrc != "") {
                        
                        //$api.attr(ele, 'src', imgSrc);
                    }
                    if (ret.hasOwnProperty("list")) {
                        
                        //var localAvaterPath = ret.data;
                        //$api.attr(ele, 'src', ret.list[0].path);
                        api.showProgress({
                            title: '正在上传...',
                            modal: true
                        });
                    }
                    var ele = $api.dom('#icon');
                    api.ajax({
                        url: webSiteUrl+'/app/public/uploads',
                        method: 'post',
                        data: {
                            files: {
                                'file': ret.data,
                            }
                        }
                    }, function(ret, err) {
                        if (ret) {
                            if (ret.code == 200) {
                                 //alert(JSON.stringify(ret));
                                 api.hideProgress();
                               
                                $api.attr(ele, 'src', webSiteUrl+ret.pic);

                                $("input[name=icon]").val(ret.id);
                                
                                    
                               

                            } else {
                                api.hideProgress();
                                api.toast({
                                    msg: ret.msg,
                                    location: 'middle'
                                });
                            }
                        } else {
                            api.hideProgress();
                            api.toast({
                                msg: err.msg,
                                location: 'middle'
                            });
                        }
                    });




                }
            });
        } else if (sourceType == 2) { // 从相机中选择
            //UIMediaScanner 是一个多媒体扫描器，可扫描系统的图片、视频等多媒体资源
            var obj = api.require('UIMediaScanner');
            obj.open({
                //返回的资源种类,picture（图片）,video（视频）,all（图片和视频）
                type: 'picture',
                //（可选项）图片显示的列数，须大于1
                column: 4,
                max: 1,
                //（可选项）图片排序方式,asc（旧->新）,desc（新->旧）
                sort: {
                    key: 'time',
                    order: 'desc'
                },
                //（可选项）模块各部分的文字内容
                texts: {
                    stateText: '已选择*项',
                    cancelText: '取消',
                    finishText: '完成'
                },
                styles: {
                    bg: '#fff',
                    mark: {
                        icon: '',
                        position: 'bottom_right',
                        size: 20
                    },
                    nav: {
                        bg: '#eee',
                        stateColor: '#000',
                        stateSize: 18,
                        cancleBg: 'rgba(0,0,0,0)',
                        cancelColor: '#000',
                        cancelSize: 18,
                        finishBg: 'rgba(0,0,0,0)',
                        finishColor: '#000',
                        finishSize: 18
                    }
                }
            }, function(ret) {
                // 获取图片数据并处理
                if (ret) {
                    if (ret.hasOwnProperty("list")) {
                        
                        //var localAvaterPath = ret.list[0].path;
                        //$api.attr(ele, 'src', ret.list[0].path);
                        api.showProgress({
                            title: '正在上传...',
                            modal: true
                        });
                    }
                    var ele = $api.dom('#icon');
                    api.ajax({
                        url: webSiteUrl+'/app/public/uploads',
                        method: 'post',
                        data: {
                            files: {
                                'file': ret.list[0].path,
                            }
                        }
                    }, function(ret, err) {
                        if(ret.eventType=='cancel'){return;}
                        if (ret) {
                            if (ret.code == 200) {
                                //alert(JSON.stringify(ret));
                                api.hideProgress();

                                $api.attr(ele, 'src', webSiteUrl+ret.pic);

                                $("input[name=icon]").val(ret.id);
                                
                                
                                

                            } else {
                                api.hideProgress();
                                api.toast({
                                    msg: ret.msg,
                                    location: 'middle'
                                });
                            }
                        } else {
                            api.hideProgress();
                            api.toast({
                                msg: err.msg,
                                location: 'middle'
                            });
                        }
                    });





                }
            });
        }
    }

function changeicon(icon){
    var p={token:token,icon:icon};
        api.ajax({
            url: webSiteUrl+'/app/user/edit_user',
            method: 'post',
            data: {
                values:p
            }
        }, function(ret, err) {
            api.hideProgress();
            
            if(ret.code==200){
                alert(ret.msg)
                
            }else{alert(ret.msg)}
        });
}
</script>

</html>
